package com.kuaiditu.assistant.module.update;

import android.support.annotation.NonNull;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.AppUtils;
import com.kuaiditu.assistant.api.assistant_api.base.AssistantRequest;
import com.vector.update_app.HttpManager;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Eenie on 2017/9/4.
 * Email: eeniework@gmail.com
 * Des:
 */

public class UpdateHttpManager implements HttpManager {


    @Override
    public void asyncGet(@NonNull String url, @NonNull Map<String, String> params, @NonNull Callback callBack) {

    }

    @Override
    public void asyncPost(@NonNull String url, @NonNull Map<String, String> params, @NonNull final Callback callBack) {
        OkHttpClient mClient = new OkHttpClient.Builder().build();
        Map<String, Object> param = new HashMap<>();
        param.put("source", "ANDROID");
        param.put("versionCode", AppUtils.getAppVersionCode());
        FormBody formBody = new FormBody.Builder()
                .add("param", AssistantRequest.DEFAULT("courierLogin.getVersionInfo", param).build())
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();

        mClient.newCall(request)
                .enqueue(new okhttp3.Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        e.printStackTrace();
                        callBack.onError(e.getMessage());
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        if (response.isSuccessful()) {
                            JSONObject resJson = JSON.parseObject(response.body().string());
                            callBack.onResponse(resJson.getJSONObject("respData").toJSONString());
                        }
                    }
                });
    }

    @Override
    public void download(@NonNull String url, @NonNull final String path, @NonNull final String fileName, @NonNull final FileCallback callback) {
        OkHttpClient mClient = new OkHttpClient.Builder().build();
        FormBody formBody = new FormBody.Builder().add("sds", "sdsd").build();
        MultipartBody body = new MultipartBody.Builder()
                .addPart(formBody)
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();

        File file = new File(path + File.pathSeparator + fileName);
        mClient.newCall(request)
                .enqueue(new com.kuaiditu.assistant.api.FileCallback(file) {
                    @Override
                    public void onBefore() {
                        callback.onBefore();
                    }

                    @Override
                    public void onProgress(long curLen, long totLen) {
                        callback.onProgress(((float)curLen )/ ((float)totLen), totLen);
                    }

                    @Override
                    public void onAfter(File file) {
                        callback.onResponse(file);
                    }
                });
    }
}
